package br.odb.bzk3.android;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Resources;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Looper;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

/**
 * @author monty
 * 
 */
public class EngineActivity extends Activity implements Runnable, SensorEventListener {
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @author monty
	 *
	 */
	private class SetContentViewRunnable implements Runnable {
		@Override
		public void run() {
			myProgressDialog.dismiss();
			Log.d("BZK3", "setting content view");
			setContentView(engineView);
		}
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	private static EngineView engineView;
	/**
	 * 
	 */
	private static ProgressDialog myProgressDialog;
	/**
	 * 
	 */
	public static int selected;
	/**
	 * 
	 */
	private static EngineActivity instance;
	/**
	 * 
	 */
	private static Resources staticResources;
	/***
	 * 
	 */
	private InputStream data;
	private InputStream detail;
	private InputStream detailMaterials;
	private SensorManager sensorManager;	
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @return
	 */
	public static EngineActivity getInstance() {
		return instance;
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @return
	 */
	public static Resources getInstanceResources() {

		return staticResources;
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param lvl
	 * @return
	 */
	private InputStream file(int lvl) {
		InputStream is = null;
		switch (lvl) {
		case 0:
			is = getResources().openRawResource(R.raw.testeless );
			break;
		case 1:
			is = getResources().openRawResource(R.raw.portas );
			break;
		case 2:
			is = getResources().openRawResource(R.raw.teste2);
			break;
		case 3:
			is = getResources().openRawResource(R.raw.level_1_4);
			break;
		case 4:
			is = getResources().openRawResource(R.raw.level_2_1);
			break;

		case 5:
			is = getResources().openRawResource(R.raw.level_2_2);
			break;

		case 6:
			is = getResources().openRawResource(R.raw.level_2_3);
			break;

		case 7:
			is = getResources().openRawResource(R.raw.level_2_4);
			break;
		case 8:
			is = getResources().openRawResource(R.raw.level_2_5);
			break;

		}
		Log.d("Willie", "Abastecendo buffer");
		StringBuffer sb = new StringBuffer();
		String tmp = "";
		try {
			int chars;
			if (is == null)
				return null;
			while ((chars = is.read()) != -1) {
				sb.append((char) chars);
			}
			tmp = sb.toString();
		} catch (Exception e) {
		}
		Log.d("Willie", "Abastecendo buffer");
		return new ByteArrayInputStream(tmp.getBytes());
	}
	
	/**
	 * 
	 * @param lvl
	 * @return
	 */
	private InputStream fileDetail(int lvl) {
		InputStream is = null;
		switch (lvl) {
		case 0:
			is = getResources().openRawResource(R.raw.levelmonkey );
			break;
		case 1:
			is = null;
			break;
		case 2:
			is = null;
			break;
		case 3:
			is = getResources().openRawResource(R.raw.level_1_4);
			break;
		case 4:
			is = getResources().openRawResource(R.raw.level_2_1);
			break;

		case 5:
			is = getResources().openRawResource(R.raw.level_2_2);
			break;

		case 6:
			is = getResources().openRawResource(R.raw.level_2_3);
			break;

		case 7:
			is = getResources().openRawResource(R.raw.level_2_4);
			break;
		case 8:
			is = getResources().openRawResource(R.raw.level_2_5);
			break;

		}
		Log.d("Willie", "Abastecendo buffer");
		StringBuffer sb = new StringBuffer();
		String tmp = "";
		try {
			int chars;
			if (is == null)
				return null;
			while ((chars = is.read()) != -1) {
				sb.append((char) chars);
			}
			tmp = sb.toString();
		} catch (Exception e) {
		}
		Log.d("Willie", "Abastecendo buffer");
		return new ByteArrayInputStream(tmp.getBytes());
	}
	
	/**
	 * 
	 * @param lvl
	 * @return
	 */
	private InputStream fileDetailMaterials(int lvl) {
		InputStream is = null;
		switch (lvl) {
		case 0:
			is = getResources().openRawResource(R.raw.levelmonkey_mtl );
			break;
		case 1:
			is = null;
			break;
		case 2:
			is = null;
			break;
		case 3:
			is = getResources().openRawResource(R.raw.level_1_4);
			break;
		case 4:
			is = getResources().openRawResource(R.raw.level_2_1);
			break;

		case 5:
			is = getResources().openRawResource(R.raw.level_2_2);
			break;

		case 6:
			is = getResources().openRawResource(R.raw.level_2_3);
			break;

		case 7:
			is = getResources().openRawResource(R.raw.level_2_4);
			break;
		case 8:
			is = getResources().openRawResource(R.raw.level_2_5);
			break;

		}
		Log.d("Willie", "Abastecendo buffer");
		StringBuffer sb = new StringBuffer();
		String tmp = "";
		try {
			int chars;
			if (is == null)
				return null;
			while ((chars = is.read()) != -1) {
				sb.append((char) chars);
			}
			tmp = sb.toString();
		} catch (Exception e) {
		}
		Log.d("Willie", "Abastecendo buffer");
		return new ByteArrayInputStream(tmp.getBytes());
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    //this.requestWindowFeature( Window.FEATURE_NO_TITLE );
        //getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );
		
		instance = this;
		staticResources = getResources();
		myProgressDialog = ProgressDialog.show(this, "loading",
				"Loading level data");

		engineView = new EngineView(this);
		Thread t = new Thread(this);
		t.start();
		
		sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_NORMAL);
		
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public void run() {
		Looper.myLooper();
		Looper.prepare();
		Log.d("Willie", "preparando leitura");
		data = file(selected);
		detail = fileDetail(selected);
		detailMaterials = fileDetailMaterials( selected );
		Log.d("Willie", "buffer criado");
		long m0 = System.currentTimeMillis();
		engineView.load(data, detail, detailMaterials );
		engineView.loadComplete();
		long m1 = System.currentTimeMillis();
		System.out.println("delta: " + (m1 - m0));

		runOnUiThread(new SetContentViewRunnable());
	}
	// ------------------------------------------------------------------------------------------------------------
	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onSensorChanged(SensorEvent event) {
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
			engineView.accel.setX( event.values[0] );
			engineView.accel.setY( event.values[1] );
			engineView.accel.setZ( event.values[2] );
		}
	}
}
